<template>
  <base-load-page :loadState="false" class="base-load-page">
    <template #head>
      <div style="padding-bottom: 15px" class="head-box">
        <div class="left-box">
          <bubble-select-local
            v-model="filterData.categoryId"
            labelKey="categoryName"
            valueKey="categoryId"
            :axios="dramaCategoryListAxios"
            placeholder="请选择分类"
            @change="getAxiosData(true)"
          ></bubble-select-local>
          <base-input-search
            v-model="filterData.dramaTitle"
            clearable
            placeholder="请输入短剧名称"
            style="width: 100%"
            maxlength="30"
            :prefixIcon="IconSearch"
            @submit="getAxiosData(true)"
          ></base-input-search>
        </div>
      </div>
    </template>
    <el-table ref="elTableRef" v-loading="baseData.loading" border height="100%" :data="tableData" showOverflowTooltip stripe style="width: 100%">
      <template #empty>
        <base-empty :description="description" :pageLoadButton="[{ text: '重新加载', callBack: () => getAxiosData() }]"></base-empty>
      </template>
      <el-table-column
        fixed
        type="index"
        :index="(index: number) => (filterData.current - 1) * filterData.size + index + 1"
        label="编号"
        sortable
        width="60"
        align="center"
        headerAlign="center"
      ></el-table-column>
      <el-table-column fixed label="短剧名称" prop="dramaTitle" minWidth="180"></el-table-column>
      <el-table-column label="追剧人数" prop="chasingUserNum" sortable minWidth="110"></el-table-column>
      <el-table-column label="上映状态" width="90">
        <template #default="{ row }">
          <el-tag :type="row.dramaStatus === 3 ? 'success' : row.dramaStatus === 2 ? 'primary' : 'info'" size="small">{{ row.dramaStatusDesc }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="dramaCategoryName" minWidth="120"></el-table-column>
      <el-table-column label="导演" prop="director" minWidth="90"></el-table-column>
      <el-table-column label="演员" prop="performer" minWidth="90"></el-table-column>
      <el-table-column label="标签" prop="tags" minWidth="120"></el-table-column>
      <el-table-column label="单集价格" prop="price" sortable minWidth="110"></el-table-column>
      <el-table-column label="单集K币" prop="virtualCoin" sortable minWidth="110"></el-table-column>
      <el-table-column label="全集价格" prop="allEpisodePrice" sortable minWidth="110"></el-table-column>
      <el-table-column label="全集K币" prop="allEpisodeVirtualCoin" sortable minWidth="110"></el-table-column>
    </el-table>
    <template #foot>
      <div class="pagination-box">
        <el-pagination
          v-model:currentPage="filterData.current"
          v-model:pageSize="filterData.size"
          :pageSizes="[20, 30, 40, 50]"
          :disabled="baseData.loading"
          :pagerCount="13"
          layout="total, prev, pager, next, sizes, jumper"
          :total="baseData.total"
          @change="getAxiosData(true)"
        ></el-pagination>
      </div>
    </template>
  </base-load-page>
</template>

<script setup lang="ts">
  import IconSearch from '~icons/ep/search';
  import { type ChasingDrama, type ChasingDramaListRequest, dramaCategoryListAxios, getChasingDramaListAxios } from '@/api/drama-management';

  const elTableRef: $RefComp<'ElTable'> = ref(null);
  const baseData = shallowReactive({
    type: 'add' as 'add' | 'edit',
    /** 加载状态 */
    loading: true,
    /** 总数量 */
    total: 0,
  });

  const filterData: Ref<ChasingDramaListRequest> = ref<ChasingDramaListRequest>({
    current: 1,
    size: 40,
    categoryId: undefined,
    dramaStatus: undefined,
    dramaTitle: '',
    status: undefined,
  });

  const tableData: Ref<ChasingDrama[]> = ref([]);
  const description = ref('');
  onMounted(() => {
    getAxiosData();
  });

  /**
   * 设置表格数据
   */
  async function getAxiosData(showLoading = true) {
    try {
      await nextTick();
      baseData.loading = Boolean(showLoading);
      const res = await getChasingDramaListAxios(filterData.value);
      tableData.value = res.data.records;
      description.value = '暂无数据';
      filterData.value.current = res.data.current;
      filterData.value.size = res.data.size;
      baseData.total = res.data.total;
      baseData.loading = false;
    } catch (error) {
      baseData.loading = false;
      tableData.value = [];
      description.value = error.msg || '加载失败，-BD001';
    }
  }
</script>

<style lang="scss" scoped>
  .pagination-box {
    @include flex(row, nowrap, center, center, center);
    padding-top: 15px;
  }

  .head-box {
    @include flex(row, nowrap, flex-start, center, center);

    .left-box {
      display: grid;
      flex: 1;
      grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
      grid-gap: 15px;
      margin-right: 15px;
    }
  }
</style>
